#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QComboBox>
#include <QSerialPortInfo>
#include <adcollector.h>
#include <QSettings>
#include "switchcontrol.h"
#include "device.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    ADCollector adCollector;
    SwitchControl switchControl;
    Device myDevice;
    Device DeviceA1,DeviceA2,DeviceB1,DeviceB2,*Device1,*Device2;
    QTimer timer;
    int workMode=0;
    void SlotsInit();                               //连接信号与槽函数
    void GetAvailablePort(QComboBox *controlItem);
    void TableWidgetAddRow(QString,int channel,QColor a=Qt::black);
    void TableWidget2AddRow(QString,int channel,QColor a=Qt::black);
    int  GetNextPartToVerify();                      //0没有 1左 2右
    void ShowTestFailed(int,QString);
    short ReVerify0(float targetvalue,float value1,short step1,float value2,short step2);
    void showGrayBumb(int value);
    int currentPart=2;
    int steps=0;
    int tryTimes11=2,tryTimes12=2,tryTimes21=2,tryTimes22=2;
    RecvAdValue zeroPoint;
    RecvAdValue fullPoint;
    RecvAdValue firstPoint,secondPoint;
    short zeroPonit1x,zeroPonit2x,fullPointx1,fullPointx2;
    bool device1IsSuccees=true;
    bool device2IsSuccees=true;
    bool device1ShouldTest=true;
    bool device2ShouldTest=true;
    float k11,k12,k21,k22;
    /*-----------------------------------测试选择--------------------------------------*/
    /*----------------------------------测试流程函数-----------------------------------*/
    bool HaveTryTime(int channel,int zero);
    int GoToStep(int channel,int zero);
    void ChooseAOrBToTest();                                //选取A组进行测试
    void BreakSwitch();                                     //断开开关
    void JudgeIfBreakSwitchSuccess();                       //判断断开开关是否成功
    void SetDeviceRangeChannel1();                          //设置通道1量程
    void SetDeviceRangeChannel2();                          //设置通道2量程
    void SetDeviceType();                                   //设置产品类型 A或者B
    void Channel1ZeroCalibration();                         //通道1输入零点校准
    void Channel2ZeroCalibration();                         //通道2输入零点校准
    void OpenSwitch();                                      //闭合继电器
    void Sleep2S();                                         //休眠2s
    void JudgeIfOpenSwitchSuccess();                        //判断打开开关是否成功
    void Channel1VerifyFull();                              //通道1输入较满值
    void Channel2VerifyFull();                              //通道2输入较满值

    void Channel1ZeroOutPutTest(int value);                 //通道1零点输出测试
    void GetFirstPoint(int tongDao);
    void GetSecondPoint(int tongDao);
    void Channel1ZeroOutPutVerify();                        //通道1输出校准
    void JudgeChannel1ZeroVerifySucess();                   //读出校准后的值

    void Channel1FullOutPutTest(int value);                 //通道1满值输出测试
    void Channel1FullOutputVerify();                        //通道1满值输出校准
    void JudgeChannel1FullVerifySuccess();                  //读出校准后的值判断校准是否成功
    void SaveChannel(int value);

    void Channel2ZeroOutputTest(int value);                 //通道2零点输出测试
    void Channel2ZeroOutputVerify();                        //通道2零点输出校准
    void JudgeChannelOutputVerifySuccess(int channel,int zero);//读出校准后的值判断是否校准成功 channel=1 通道1 =2通道2 zero=1校准零点 zero=2校准满值

    void Channel2FullOutputTest(int value);                 //通道2满值输出测试
    void Channel2FullOutputVerify();                        //通道2满值输出校验
    void JudgeChannel2FullVerifySuccess();                  //判断通道2满值输出是否校准成功
    /********************************测试流程函数****************************************/
    /********************************第二种测试模式**************************************/
    int Device1GoToNext();
    int Device2GoToNext();

    void ChooseAOrBToTest2();                                //选取A组进行测试


    void BreakSwitch1Or3();
    void BreakSwitch2Or4();

    void If1Or3BreakSucess();
    void If2OR4BreakSucess();

    void SetDevice1RangeChannel1();
    void SetDevice2RangeChannel1();

    void SetDevice1RangeChannel2();
    void SetDevice2RangeChannel2();

    void SetDevice1DeviceType();
    void SetDevice2DeviceType();

    void Device1Channel1ZeroCalibration();
    void Device2Channel1ZeroCalibration();

    void Device1Channel2ZeroCalibration();
    void Device2Channel2ZeroCalibration();

    void Device1OpenSwitch();
    void Device2OpenSwicth();

    void Device1Sleep500ms();
    void Device2Sleep500ms();

    void If1Or3OpenSucess();
    void If2Or4OpenSucess();

    void Device1Channel1VerifyFull();
    void Device2Channel1VerifyFull();

    void Device1Channel2VerifyFull();
    void Device2Channel2VerifyFull();

    void Device1Channel1ZeroOutPutTest(int value);
    void Device2Channel1ZeroOutPutTest(int value);

    void Device1GetFirstPoint(int channel);
    void Device2GetFirstPoint(int channel);

    void Device1GetSecondPoint(int channel);
    void Device2GetSecondPoint(int channel);

    void Device1Channel1ZeroOutPutVerify();
    void Device2Channel1ZeroOutPutVerify();

    void Device1JudgeChannelOutPutVerify(int channel,int zero);
    void Device2JudgeChannelOutPutVerify(int channel,int zero);

    void Device1Channel1FullOutPutTest(int value);
    void Device2Channel1FullOutPutTest(int value);

    void Device1Channel1FullOutPutVerify();
    void Device2Channel1FullOutPutVerify();

    void Device1SaveChannel(int channel);
    void Device2SaveChannel(int channel);

    void Device1Channel2ZeroOutputTest(int value);
    void Device2Channel2ZeroOutputTest(int value);

    void Device1Channel2ZeroOutputVerify();
    void Device2Channel2ZeroOutputVerify();

    void Device1Channel2FullOutputTest(int value);
    void Device2Channel2FullOutputTest(int value);

    void Device1Channel2FullOutputVerify();
    void Device2Channel2FullOutputVerify();

    void TableWidgetIsClear();

    int  GetTime(int stepID);


    void SleepEnoughTime();
    int testStep=0;
    int innerStep1=0;
    int innerStep2=0;
    int hasSleepMSTime=0;

    bool device1IsFinish2=false;
    bool device2IsFinish2=false;
    bool device1IsSuccess2=false;
    bool device2IsSuccess2=false;

    RecvAdValue firstPoint1,secondPoint1,firstPoint2,secondPoint2;
    /********************************第二种测试模式**************************************/
private:
    Ui::MainWindow *ui;
public slots:
    /********************************第二种测试模式**************************************/
    void TimerTimeOut2();
    void GetReady();
   /********************************第二种测试模式**************************************/

    void OpenPort();
    void OpenSwitchControlDevice();
    void ShowAdValue(float *);
    void HandleGetADValue();
    void OpenSwitch1();
    void OpenSwitch2();
    void OpenSwitch3();
    void OpenSwitch4();
    void CloseSwitch1();
    void CloseSwitch2();
    void CloseSwitch3();
    void CloseSwitch4();
    void StartAsk();
    void GetSwitchInputValue();
    void GetSwitchControlValue();
    void OpenDevice();
    void SendFc6ToDevice();
    void TimerTimeOut();
    void Start();
    void CurrentMode();
    void VoltageMode();

    void ZeroVerify();
    void ShowBumb(int);
private slots:
    void SetWitchDeviceToTest();
    void on_pushButton_18_clicked();
};
#endif // MAINWINDOW_H
